package com.panshi.controller;

import com.google.common.collect.Maps;
import com.panshi.api.SysUserApi;
import com.panshi.vo.R;
import com.panshi.vo.SysUserVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/sysuser")
public class UserController {

    @Autowired
    private SysUserApi sysUserApi;

    @RequiresPermissions("user:list:*")
    @GetMapping("/pageUserList")
    public Map<String,Object> pageUserList(Integer page,Integer limit){
        R r = sysUserApi.pageUserList(page, limit);
        Map<String,Object> map = Maps.newHashMap();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",r.getData().get("total"));
        map.put("data",r.getData().get("rows"));
        return map;
    }

    @RequiresPermissions("user:add:*")
    @PostMapping
    public R addUser(@RequestBody SysUserVO sysUserVO){
        return sysUserApi.saveSysUser(sysUserVO);
    }

    @RequiresPermissions("user:edit:*")
    @PutMapping
    public R editUser(@RequestBody SysUserVO sysUserVO){
        return sysUserApi.updateSysUser(sysUserVO);
    }

    @RequiresPermissions("user:del:*")
    @DeleteMapping("/{id}")
    public R delUser(@PathVariable("id")Integer id){
        return sysUserApi.deleteSysUser(id);
    }
}
